﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.SCA.EntidadesNegocio;
using MJM.CBD.Conn;

namespace MJM.SCA.AccesoDatos
{
    public class LogwsAD
    {
        public List<LogwsEN> Listar(String IP,String Servicio,String Metodo,String FechaIni,String FechaFin,String Resultado)
        {
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaLog());
            SqlCommand cmd = new SqlCommand("sp_s_T_LOGWS_CLIENT_Listar", conn);
            cmd.CommandType=CommandType.StoredProcedure;
            SqlParameter prmIP, prmServicio, prmMetodo, prmFecIni, prmFecFin, prmResultado;
            List<LogwsEN> lst = new List<LogwsEN>();
            SqlDataReader dr;
            try
            {
                prmFecFin = cmd.CreateParameter();
                prmFecFin.ParameterName = "@vLOGSWS_chFECMODFIN";
                prmFecFin.SqlDbType = SqlDbType.VarChar;
                prmFecFin.Value = FechaFin;
                cmd.Parameters.Add(prmFecFin);

                prmFecIni = cmd.CreateParameter();
                prmFecIni.ParameterName = "@vLOGSWS_chFECMODINI";
                prmFecIni.SqlDbType = SqlDbType.VarChar;
                prmFecIni.Value = FechaFin;
                cmd.Parameters.Add(prmFecIni);

                prmMetodo = cmd.CreateParameter();
                prmMetodo.ParameterName = "@vLOGSWS_chNOMMET";
                prmMetodo.SqlDbType = SqlDbType.VarChar;
                prmMetodo.Value = Metodo;
                cmd.Parameters.Add(prmMetodo);

                prmServicio = cmd.CreateParameter();
                prmServicio.ParameterName = "@vLOGSWS_chNOMSER";
                prmServicio.SqlDbType = SqlDbType.VarChar;
                prmServicio.Value = Servicio;
                cmd.Parameters.Add(prmServicio);

                prmResultado = cmd.CreateParameter();
                prmResultado.ParameterName = "@vLOGSWS_chRESINT";
                prmResultado.SqlDbType = SqlDbType.VarChar;
                prmResultado.Value = Resultado;
                cmd.Parameters.Add(prmResultado);

                prmIP = cmd.CreateParameter();
                prmIP.ParameterName = "@vLOGWS_chIPCLIEN";
                prmIP.SqlDbType = SqlDbType.VarChar;
                prmIP.Value = IP;
                cmd.Parameters.Add(prmIP);

                cmd.Connection.Open();
                dr=cmd.ExecuteReader();
                LogwsEN obj;
                while (dr.Read())
                { 
                    obj = new LogwsEN();
                    obj.LOGWS_chIPCLIEN = dr.GetString(1);
                    obj.LOGWS_chFECREG = dr.GetString(2);
                    obj.LOGSWS_CHHORREG = dr.GetString(3);
                    try
                    {
                        obj.LOGSWS_chNOMSER = dr.GetString(4);
                    }
                    catch
                    {
                        obj.LOGSWS_chNOMSER = "";
                    }
                    try
                    {
                        obj.LOGSWS_chNOMMET = dr.GetString(5);
                    }
                    catch
                    {
                        obj.LOGSWS_chNOMMET = "";
                    }
                    try
                    {
                        obj.LOGSWS_chRESINT = dr.GetString(6);
                    }
                    catch
                    {
                        obj.LOGSWS_chRESINT = "";
                    }
                    try
                    {
                        obj.LOGSWS_chDESFAL = dr.GetString(7);
                    }
                    catch
                    {
                        obj.LOGSWS_chDESFAL = "";
                    }
                    try
                    {
                        obj.LOGSWS_chFECMOD = dr.GetString(8);
                    }
                    catch
                    {
                        obj.LOGSWS_chFECMOD = "";
                    }
                    lst.Add(obj);
                }
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                conn.Dispose();
                cmd.Dispose();
                return lst;
            }
            return lst;
        }
    }
}
